/*
 * Copyright (C) 2010 Young, Fey <fey.young@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.youngfey.ebs

import org.junit.*
import static org.junit.Assert.*

/**
 * This is not a test but a demo, to run it set the demo flag to true
 * @author Young, Fey <fey.young@gmail.com>
 */
class DemoTest {
    def demo = true

    @Test
    void test() {
        if(!demo) {
            return
        }
        // The Timesheet


        // Histories
        def histories = []
        120.times {
            histories << new History(rand(20), Math.random())
        }
        printInfo('Histories', histories)

        // Your Estimates
        def estimator = new Estimator()
        estimator.histories = histories

        // EBS Results
        def analyst = new Analyst()
        def steps = []
        def stepsNum = 10
        stepsNum.times { steps << rand(20) }
        printInfo('Your Estimates', steps.toString())

        

        println "Estimate MAX = ${histories.collect({ it.velocity }).max() * steps.sum()}"
        println "Estimate MIN = ${histories.collect({ it.velocity }).min() * steps.sum()}"
        println '__________________________________'

        [10, 100, 1000, 10000, 100000].each { r->
            3.times {
                time()
                estimator.repeats = r
                def es = estimator.estimate(steps)
                print 'Estimate: '
                println time()
                analyst.estimates = es 
                print 'Analyze: '
                println time()
                printInfo("Brief: repeats = $r", analyst.allStepsBrief)
            }
        }

    }
    def last = new Date()

    def time() {
        def now = new Date()
        def time = "${(now.time - last.time) / 1000} sec"
        last = now
        time
    }

    void printInfo(title, msg) {
        println "******************* $title *********************"
        if(msg instanceof Collection) {
            msg.each { println it }
        } else {
            println msg
        }
        println ''
    }

    def rand(v) {
        def n = BigDecimal.valueOf(Math.random() * v).setScale(1, BigDecimal.ROUND_HALF_UP) 
        n > 0 ? n : rand(v)
    }
}

